package com.controller;

import com.bean.*;
import com.dao.FlowerDao;
import com.dao.impl.FlowerDaoImpl;
import com.service.OrderItemService;
import com.service.OrderService;
import com.service.impl.OrderItemServiceImpl;
import com.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class OrderServlet extends BaseServlet {

    private OrderService orderService=new OrderServiceImpl();
    private OrderItemService orderItemService=new OrderItemServiceImpl();


    /**
     * 普通用户下订单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取cart购物车对象
        Cart cart= (Cart) req.getSession().getAttribute("cart");

        //获取userid
        User loginUser=(User)req.getSession().getAttribute("user");
        if(loginUser==null){
            req.getRequestDispatcher("/page/loginUser/login.jsp").forward(req,resp);
            return;
        }
        String userid=loginUser.getUserid();
        //调用
        String orderid=orderService.createOrder(cart,userid);
        // req.getRequestDispatcher("/page/user/check.jsp").forward(req,resp);
        req.getSession().setAttribute("orderid",orderid);
       // resp.sendRedirect(req.getContextPath()+"/page/user/checkOrder.jsp");

        resp.sendRedirect(req.getContextPath()+"/orderServlet?action=showMyOrders&userid="+userid);
    }

    /**
     *查看我的所有订单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showMyOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String userid=req.getParameter("userid");
        User loginUser=(User)req.getSession().getAttribute("user");
        if(loginUser==null){
            req.getRequestDispatcher("/page/loginUser/login.jsp").forward(req,resp);
            return;
        }
        List<Order> myorders = orderService.showMyOrders(userid);
        req.setAttribute("myorders",myorders);

        req.getRequestDispatcher("/page/user/checkOrder.jsp").forward(req,resp);
    }

    /**
     *查看我具体状态的订单 待发货、待签收、已签收
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showMyOrdersa(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String userid=req.getParameter("userid");
        String s=req.getParameter("status");
        int status=Integer.parseInt(s);
        System.out.println(status);
        User loginUser=(User)req.getSession().getAttribute("user");
        if(loginUser==null){
            req.getRequestDispatcher("/page/loginUser/login.jsp").forward(req,resp);
            return;
        }


        List<Order>myorders = orderService.showMyOrdersa(userid,status);

        //System.out.println(myorders);
          /*  for(Order o:myorders){
                System.out.println(o);
            }*/
        req.setAttribute("myorders",myorders);

        req.getRequestDispatcher("/page/user/checkOrder.jsp").forward(req,resp);
    }

    /**
     * 用户查看订单详情
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showOrderDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String orderid=req.getParameter("orderid");
        User loginUser=(User)req.getSession().getAttribute("user");
        if(loginUser==null){
            req.getRequestDispatcher("/page/loginUser/login.jsp").forward(req,resp);
            return;
        }
        //List<OrderItem> orderItems= null;
        List<Map<String ,Object>>orderItems=null;
        try {
           // orderItems = orderItemService.showMyOrderItem(orderid);
            orderItems=orderItemService.showOrderItem(orderid);
            req.setAttribute("orderItems",orderItems);
            req.getRequestDispatcher("/page/user/checkOrderDetail.jsp").forward(req,resp);
            System.out.println(orderItems);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
